
package com.xiaoma.myaudience.biz.adapter;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.xiaoma.myaudience.R;
import com.xiaoma.myaudience.biz.model.HomeInfoModel;
import com.xiaoma.myaudience.biz.view.LoadingImageView;
import com.xiaoma.myaudience.util.Pair;
import com.xiaoma.myaudience.util.PicUtils;
import com.xiaoma.myaudience.util.model.ModelUtils;

public class HotAdapter extends BaseAdapter implements View.OnClickListener {
//    private ArrayList<Pair<String, ArrayList<HotVideoInfo>>> mList;
    private List<Pair<String, List<Map<String, Object>>>> mList;	

    private Context mContext;

    private LayoutInflater mInflater;
    
    private String mSizeStr;

    public HotAdapter(Context context, List<Pair<String, List<Map<String, Object>>>> list) {
        mList = list;
        mInflater = LayoutInflater.from(context);
        mContext = context;
        mSizeStr = PicUtils.composeSize((int) context.getResources().getDimension(
                R.dimen.list_item_img_width), (int) context.getResources().getDimension(
                R.dimen.list_item_img_height));
    }

    @Override
    public int getCount() {
        int count = mList.size();
        for (Pair<String, List<Map<String, Object>>> pair : mList) {
            count += pair.second.size();
        }
        return count;
    }

    @Override
    public Object getItem(int position) {
        ItemPosition itemPosition = getRealPosition(position);
        return mList.get(itemPosition.group).second.get(itemPosition.pos);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ItemPosition itemPosition = getRealPosition(position);
        if (itemPosition.pos == -1) {
            return getGroupView(itemPosition.group, convertView, parent);
        } else {
            return getChildView(itemPosition.group, itemPosition.pos, convertView, parent);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return isGroup(position) ? 1 : 0;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public boolean isEnabled(int position) {
        return !isGroup(position);
    }

    /**
     * 判断这个位置是否是group
     * 
     * @param position
     * @return
     */
    private boolean isGroup(int position) {
        ItemPosition itemPosition = getRealPosition(position);
        return itemPosition.pos == -1;
    }
    
    /**
     * 返回item真实位置
     * @param position
     * @return
     */
    private ItemPosition getRealPosition(int position) {
        ItemPosition itemPosition = new ItemPosition();
        int groupPosition = 0;
        final int N = mList.size();
        for (int i = 0; i < N; i++) {
            itemPosition.group = i;
            final int size = mList.get(i).second.size();
            if ((groupPosition + size) >= position) {
                itemPosition.pos = position - groupPosition - 1;
                break;
            } else {
                groupPosition += (size + 1);
            }
        }

        return itemPosition;

    }
    

    public Object getChild(int groupPosition, int childPosition) {
        return mList.get(groupPosition).second.get(childPosition);
    }

    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    public View getChildView(int groupPosition, int childPosition, View convertView,
            ViewGroup parent) {
    	if (groupPosition == 0) {
			return getFirstChildView(groupPosition, childPosition, convertView);
		} else {
			return getOtherChildView(groupPosition, childPosition, convertView);
		}
    }
    
    private View getFirstChildView(int groupPosition, int childPosition, View convertView) {
    	if (convertView == null) {
            convertView = mInflater.inflate(R.layout.video_list_item, null);
            ChildViewHolder holder = new ChildViewHolder();
            holder.img = (LoadingImageView) convertView.findViewById(R.id.img);
            holder.score = (TextView) convertView.findViewById(R.id.score);
            holder.ratingBar = (RatingBar) convertView.findViewById(R.id.rating_bar);
            holder.title = (TextView) convertView.findViewById(R.id.title);
            holder.zcr = (TextView) convertView.findViewById(R.id.zcr);
            holder.date = (TextView) convertView.findViewById(R.id.date);
            
            convertView.setTag(holder);
        }

        ChildViewHolder holder = (ChildViewHolder) convertView.getTag();
        Map<String, Object> childMap = (Map<String, Object>) getChild(groupPosition, childPosition);
        
        holder.img.setLoadingDrawable(ModelUtils.getStringValue(childMap, HomeInfoModel.PARAM_POSTER), mSizeStr);        
        holder.title.setText(ModelUtils.getStringValue(childMap, HomeInfoModel.PARAM_TITLE));
        String score = ModelUtils.getStringValue(childMap, HomeInfoModel.PARAM_STARLEVEL);
        holder.score.setText(String.valueOf(score));
        Float scoreFloat = Float.valueOf(score); 
        holder.ratingBar.setRating(scoreFloat == null ? 0.0f : scoreFloat);
        holder.zcr.setText("主持人：" + ModelUtils.getStringValue(childMap, HomeInfoModel.PARAM_ZCR));
        holder.date.setText("时间：" + ModelUtils.getStringValue(childMap, HomeInfoModel.PARAM_STARTTIME));
        
        return convertView;
    }
    
    private View getOtherChildView(int groupPosition, int childPosition, View convertView) {
    	if (convertView == null) {
            convertView = mInflater.inflate(R.layout.video_list_other_item, null);
            ChildViewHolder holder = new ChildViewHolder();
            holder.img = (LoadingImageView) convertView.findViewById(R.id.img);
            holder.score = (TextView) convertView.findViewById(R.id.score);
            holder.ratingBar = (RatingBar) convertView.findViewById(R.id.rating_bar);
            holder.title = (TextView) convertView.findViewById(R.id.title);
            holder.zcr = (TextView) convertView.findViewById(R.id.zcr);
            holder.date = (TextView) convertView.findViewById(R.id.date);
            
            convertView.setTag(holder);
        }

        ChildViewHolder holder = (ChildViewHolder) convertView.getTag();
        Map<String, Object> childMap = (Map<String, Object>) getChild(groupPosition, childPosition);
        
        holder.img.setLoadingDrawable(ModelUtils.getStringValue(childMap, HomeInfoModel.PARAM_PATH), mSizeStr);        
        holder.title.setText(ModelUtils.getStringValue(childMap, HomeInfoModel.PARAM_TITLE));
        if (groupPosition == 1) {
        	String score = ModelUtils.getStringValue(childMap, HomeInfoModel.PARAM_HOT);
        	holder.score.setText(String.valueOf(score));
        	Float scoreFloat = Float.valueOf(score) / 10.0f; 
        	holder.ratingBar.setRating(scoreFloat == null ? 0.0f : scoreFloat);
        	holder.score.setVisibility(View.VISIBLE);
			holder.ratingBar.setVisibility(View.VISIBLE);
		} else {
			holder.score.setVisibility(View.GONE);
			holder.ratingBar.setVisibility(View.GONE);
		}
        holder.zcr.setText("发布者：" + ModelUtils.getStringValue(childMap, HomeInfoModel.PARAM_USERNAME));
        holder.date.setText("时间：" + ModelUtils.getStringValue(childMap, HomeInfoModel.PARAM_DATETIME));
        
        return convertView;
    }

    public int getChildrenCount(int groupPosition) {
        return mList.get(groupPosition).second.size();
    }

    public Object getGroup(int groupPosition) {
        return mList.get(groupPosition).first;
    }

    public int getGroupCount() {
        return mList.size();
    }

    public long getGroupId(int groupPosition) {
        return 0;
    }

    public View getGroupView(int groupPosition, View convertView,
            ViewGroup parent) {
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.hot_group_item, null);
            GroupViewHolder holder = new GroupViewHolder();
            holder.groupTitleTextView = (TextView) convertView.findViewById(R.id.group_title_tv);
            convertView.setTag(holder);
        }
        GroupViewHolder holder = (GroupViewHolder) convertView.getTag();
        holder.groupTitleTextView.setText((String) getGroup(groupPosition));
        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.play: {

                break;
            }
        }

    }
    
    private class ItemPosition {
        int group;

        // 如果pos<0，说明是group
        int pos = -1;
    }

    private class GroupViewHolder {
        TextView groupTitleTextView;
    }

    private class ChildViewHolder {
        LoadingImageView img;
        TextView score;
        RatingBar ratingBar;
        TextView title;
        TextView zcr;
        TextView date;
    }
}
